﻿using System;

namespace SAEA.Sockets.Tests.Server
{
    class Program
    {
        static void Main(string[] args)
        {
            TestMethod1();
            Console.WriteLine("server已启动 192.168.3.38:5100...");
            Console.ReadLine();
        }

        static IServerSokcet server = null;
        public static void TestMethod1()
        {
            var options = SocketOptionBuilder.Instance.SetCount(10000 * 10).SetPort(5100).UseIocp().Build();
            server = SocketFactory.CreateServerSocket(options);
            server.Start();
            server.OnAccepted += Server_OnAccepted;
            server.OnDisconnected += Server_OnDisconnected;
            server.OnReceive += Server_OnReceive;

        }

        private static void Server_OnReceive(Interface.ISession currentSession, byte[] data)
        {
            var count = server.ClientCounts;
            Console.WriteLine($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")} 接受到消息:{currentSession.ID},当前连接数:{count},内容:{System.Text.Encoding.UTF8.GetString(data)}");

        }

        private static void Server_OnDisconnected(string ID, Exception ex)
        {
            var count = server.ClientCounts;
            Console.WriteLine($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")} 连接已断开:{ID},当前连接数:{count}");
        }

        private static void Server_OnAccepted(object obj)
        {
            var count = server.ClientCounts;
            Console.WriteLine($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")} 接收到新连接,当前连接数:{count}");
        }
    }
}
